package future;

import com.google.common.util.concurrent.*;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Test06 {
    public static void main(String[] args) {
        ListeningExecutorService service = MoreExecutors.newDirectExecutorService();
        ExecutorService pool = Executors.newCachedThreadPool();
        Future future = pool.submit(() -> "complete!");
        ListenableFuture<?> adaptedFuture = JdkFutureAdapters.listenInPoolThread(future);
        Futures.addCallback(adaptedFuture, new FutureCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {
                System.out.println(o);
                pool.shutdown();
            }
            @Override
            public void onFailure(Throwable throwable) {
                throwable.printStackTrace();
            }
        }, service);
    }
}
